"use strict";
/* Controllers */
app.controller("noOwnerTableInfoController2", ['$scope', '$compile', '$location', '$rootScope', '$routeParams', 'noOwnerTableInfoService', function ($scope, $compile, $location, $rootScope, $routeParams, noOwnerTableInfoService) {

    var vm = $scope.vm;
    vm.p = {};
    vm.result = {};
    vm.p.dt = $routeParams.dt;

    var datePicker = $('.date');
    datePicker.datetimepicker({
        locale: 'zh-cn',
        format: 'YYYY-MM-DD'
    });
    datePicker.on('dp.hide', function (e) {
        var currentInput = $(e.currentTarget).find('input');
        var eName = currentInput.attr('name');
        var p = $scope.vm.p;
        p[eName] = currentInput.val();
        $scope.$apply()
    });

    if (typeof (vm.p.dt) == 'undefined' || vm.p.dt == '' || vm.p.dt == '0') {
        vm.p.dt = new Date(new Date().getTime() - 86400000).format('yyyy-MM-dd');
    }

    var errorFn = function (result) {
        alert(result);
    };

    // 添加白名单的方法
    $scope.confirmAddWhiteList = function (ths, dbName, tableName, tableId, permitTableId) {
        $(ths).attr("disabled", true);
        var params = {};
        if(permitTableId){
            params.id = permitTableId;
            var delprocFn = function (response) {
                if (response.success) {
                    // 设置行的permitTableId为空
                    getRow(tableId).permitTableId = "";
                    $(ths).attr("disabled", false);
                    $(ths).text('加入白名单')
                    $(ths).removeClass('btn-danger').addClass('btn-success')
                } else {
                    alert(response.msg);
                    // $(ths).attr("disabled", false);
                }
            };
            noOwnerTableInfoService.removePermitList(params, delprocFn, errorFn);
        }else {
            params.tableId = tableId;
            params.tableName = dbName + "." + tableName;
            var addprocFn = function (response) {
                if (response.success && response.data) {
                    // 设置行的permitTableId为返回值 response.data
                    getRow(tableId).permitTableId = response.data;
                    $(ths).attr("disabled", false);
                    $(ths).text('取消白名单')
                    $(ths).removeClass('btn-success').addClass('btn-danger')
                } else {
                    alert(response.msg);
                    // $(ths).attr("disabled", false);
                }
            };
            noOwnerTableInfoService.addPermitList(params, addprocFn, errorFn);
        }

        var getRow = function (tableId) {
            var res = {};
            for(var item in $scope.vm.result.data){
                if(item.tableId == tableId){
                    res = item;
                }
            }
            return res;
        }
    }

    $scope.queryReport = function () {
        $scope.listHealthScoreAppid();
    };

    $scope.listHealthScoreAppid = function () {
        $("#refreshTable").addClass('disabled');
        $("#list_table").bootstrapTable('showLoading')
        var vm = $scope.vm;
        var params = vm.p;

        var procFn = function (response) {
            var data = response.data;
            $('#list_table').bootstrapTable('hideLoading');
            $("#refreshTable").removeClass('disabled');
            if (data == null) {
                return;
            }
            vm.result.data = data;
            $('#list_table').bootstrapTable('load', data);
        };
        noOwnerTableInfoService.list(params, procFn, errorFn);
    };

    $scope.initializeController = function () {
        var vm = $scope.vm;

        var $resultTable = $('#list_table');
        $resultTable.bootstrapTable({
            pageList: [30, 50, 100, 300, 500, 1000], pageSize: 30
        });
        // $resultTable.bootstrapTable('hideColumn', 'dataContent');
        // $resultTable.bootstrapTable('hideColumn', 'tag');
        //        $resultTable.bootstrapTable('hideColumn', 'weight');


        $scope.listHealthScoreAppid();
    };

    $scope.initializeController();

}]);

/* Services */
app.service('noOwnerTableInfoService', ['ajaxService', function (ajaxService) {

    this.list = function (params, successFunction, errorFunction) {
        ajaxService.AjaxPost(params, "/star/api/basic-service/v1/noOwnerTableInfo/list.do", successFunction, errorFunction);
    };
    this.addPermitList = function (params, successFunction, errorFunction) {
        ajaxService.AjaxPost(params, "/star/api/basic-service/v1/noOwnerTableInfo/addPermitList.do", successFunction, errorFunction);
    };
    this.removePermitList = function (params, successFunction, errorFunction) {
        ajaxService.AjaxPost(params, "/star/api/basic-service/v1/noOwnerTableInfo/removePermitList.do", successFunction, errorFunction);
    };
}]);

function healthScoreListOpt(value, row, index) {
    var btnText = '加入白名单';
    var btnClass = 'success';

    if (row.permitTableId != null) {
        btnText = '取消白名单';
        btnClass = 'danger';
    }
    var btn2 = [
        '<a type="button" class="btn btn-xs btn-' + btnClass + '"',
        'onclick=angular.element(this).scope().confirmAddWhiteList(this,"', row.dbName, '","', row.tableName, '","', row.tableId, '","', row.permitTableId||'', '")>', btnText,
        '</a></div>'
    ].join('');

    return [
        btn2
    ].join('');
}
